//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Controls
{

	public class FileGalleryImageButton : WebControl 
	{
		CSContext csContext = CSContext.Current;
		User user;


		public FileGalleryImageButton() 
		{
			user = csContext.User;
		}

		public FileGalleryImageButton(CSButtonType buttonType)
		{
			this.ButtonType = buttonType;
		}

		public virtual string ApplicationKey
		{
			get
			{
				Object state = ViewState["ApplicationKey"];
				if(state != null)
					return (string)state;
				return null;
			}
			set { ViewState["ApplicationKey"] = value; }
		}

		protected override void Render(HtmlTextWriter writer) 
		{
			Folder folder = null;
			if (!Globals.IsNullorEmpty(this.ApplicationKey))
				folder = Folders.GetFolder(this.ApplicationKey);
			else
				folder = Folders.GetFolder(csContext.ApplicationKey);

			if(folder == null)
				return;

			string target = string.Empty;
			string alt = string.Empty;
			string text = string.Empty;

			// What type of button are we displaying?
			//
			switch (ButtonType) 
			{
				case CSButtonType.Upload:

					if (Permissions.ValidatePermissions( folder, Permission.Post, csContext.User ))
					{
						NavigateUrl = FileGalleryUrls.Instance().EntryAdmin(folder.ApplicationKey, -1);
						text = FileGalleryResourceManager.GetString("Files_Button_Upload");
					}
					else
					{
						return;
					}

					break;
					
					// Delete
				case CSButtonType.Delete:

					if (user.Username != Post.Username || user.IsAnonymous)
						return;

					if (this.Post.IsLocked && this.Post.Username != user.Username)
					{
						return;
					}

					if ((!folder.IsModerated || user.ModerationLevel == ModerationLevel.Unmoderated) && Permissions.ValidatePermissions( folder, Permission.Post, csContext.User, this.Post ))
					{
						NavigateUrl = FileGalleryUrls.Instance().DeleteEntry(folder.ApplicationKey, this.Post.PostID);
						text = FileGalleryResourceManager.GetString("Files_Button_Delete");
					}
					else
					{
						return;
					}

					break;

					// Edit
				case CSButtonType.Edit:

					if (user.Username != Post.Username || user.IsAnonymous)
						return;

					if (this.Post.IsLocked && this.Post.Username != user.Username)
					{
						return;
					}

					if ((!folder.IsModerated || user.ModerationLevel == ModerationLevel.Unmoderated) && Permissions.ValidatePermissions( folder, Permission.Post, csContext.User, this.Post ))
					{
						NavigateUrl = FileGalleryUrls.Instance().EntryAdmin(folder.ApplicationKey, this.Post.PostID);
						text = text = FileGalleryResourceManager.GetString("Files_Button_Edit");;
					}
					else
					{
						return;
					}

					break;

				case CSButtonType.Download:

					NavigateUrl = FileGalleryUrls.Instance().Download((Entry) this.Post);
					text = text = FileGalleryResourceManager.GetString("Files_Button_Download");;

					break;
					
			}
                        
			writer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);
			writer.AddAttribute("class", this.CssClass);
			writer.AddAttribute("style", "background-image: url(" + Globals.GetSkinPath() + "/images/file/" + ButtonType.ToString() + ".gif);");
			writer.RenderBeginTag(HtmlTextWriterTag.A);
			writer.Write(text);
			writer.RenderEndTag();
		}

		
		#region Properties

		[
		System.ComponentModel.DefaultValue( CSButtonType.Upload ),
		]
		public virtual CSButtonType ButtonType 
		{
			get 
			{
				Object state = ViewState["ButtonType"];
				if ( state != null ) 
				{
					return (CSButtonType)state;
				}
				return CSButtonType.Upload;
			}
			set 
			{
				ViewState["ButtonType"] = value;
			}
		}

		public Post Post 
		{
			get 
			{
				if (_post == null) 
				{
					Object state = ViewState["Post"];
					if (state != null) 
					{
						Int32 postID = (Int32)state;
						_post = Entries.GetEntry(csContext.ApplicationKey, postID);
					}
					else if (csContext.PostID != -1)
					{
						_post = Entries.GetEntry(csContext.ApplicationKey, csContext.PostID);
					}
				}
				return _post;
			}
			set 
			{
				_post = value;
				if (_post != null) 
				{
					ViewState["Post"] = _post.PostID;
				} 
				else 
				{
					ViewState.Remove("Post");
				}
			}
		}
        
		Post _post;

        

		[
		System.ComponentModel.DefaultValue( false ),
		]
		public virtual Boolean OpenInNewWindow 
		{
			get 
			{
				Object state = ViewState["OpenInNewWindow"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return false;
			}
			set 
			{
				ViewState["OpenInNewWindow"] = value;
			}
		}


		[
		System.ComponentModel.DefaultValue( "" ),
		]
		public virtual String NavigateUrl 
		{
			get 
			{
				Object state = ViewState["NavigateUrl"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["NavigateUrl"] = value;
			}
		}

		[
		System.ComponentModel.DefaultValue("CommonImageTextButton")
		]
		public override String CssClass
		{
			get
			{
				Object state = ViewState["CssClass"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "CommonImageTextButton";
			}
			set
			{
				ViewState["CssClass"] = value;
			}
		}

		#endregion

	}

	public enum CSButtonType 
	{
		Upload,
		Delete,
		Edit,
		Download
	}
}
